package com.itheima.reggie.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.reggie.dao.CategoryDao;
import com.itheima.reggie.dao.DishDao;
import com.itheima.reggie.dao.SetmealDao;
import com.itheima.reggie.dao.SetmealDishDao;
import com.itheima.reggie.entity.*;
import com.itheima.reggie.entity.dto.SetmealDto;
import com.itheima.reggie.entity.vo.SetmealDishVo;
import com.itheima.reggie.exception.CustomerException;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired(required = false)
    private SetmealDao setmealDao;
    @Autowired(required = false)
    private SetmealDishDao setmealDishDao;
    @Autowired(required = false)
    private CategoryDao categoryDao;
    @Autowired(required = false)
    private DishDao dishDao;

    @Override
    @CacheEvict(value = "setmealCache",allEntries =true ) // allEntries =true 该名称空间下的所有缓存全部清楚
    public void add(SetmealDto setmealDto) {
        // 拼装时间数据，添加套餐基本信息
        setmealDto.setCreateTime(LocalDateTime.now());
        setmealDto.setUpdateTime(LocalDateTime.now());
        setmealDto.setStatus(1);
        setmealDao.add(setmealDto);

        // 添加套餐菜品信息
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealDto.getId()); //所属的套餐
            setmealDish.setSort(0); //排序
            setmealDish.setCreateUser(setmealDto.getCreateUser());
            setmealDish.setUpdateUser(setmealDto.getUpdateUser());
            setmealDish.setCreateTime(setmealDto.getCreateTime());
            setmealDish.setUpdateTime(setmealDto.getUpdateTime());
        }
        setmealDishDao.batchSave(setmealDishes);
    }

    @Override
    public Page<SetmealDto> page(Integer page, Integer pageSize, String name) {

        // 设置当前页及分页大小
        PageHelper.startPage(page, pageSize);
        // 获取基本数据
        List<Setmeal> setmealList = setmealDao.getByPage(name);
        // 构造pageInfo
        PageInfo<Setmeal> pageInfo = new PageInfo<>(setmealList);

        // stream流拼装其他数据
        List<SetmealDto> setmealDtoList = setmealList.stream().map(setmeal -> {
            // setneal对象转换成setmealDto
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(setmeal, setmealDto);
            // 获取分类名称
            Category category = categoryDao.findByCateId(setmeal.getCategoryId());
            setmealDto.setCategoryName(category.getName());
            return setmealDto;
        }).collect(Collectors.toList());

        // 分装Page对象
        Page<SetmealDto> setmealDtoPage = new Page<>(setmealDtoList, pageInfo.getTotal(), pageInfo.getPageSize(), pageInfo.getPageNum());

        return setmealDtoPage;
    }

    @Override
    public void delByIds(List<Long> ids) {
        // 查询是否有在售的套餐
        int count = setmealDao.findCountByIds(ids);
        if (count > 0) {
            throw new CustomerException("套餐目前处于在售状态，没法直接删除");
        }
        // 删除套餐信息
        setmealDao.delByIds(ids);
        // 删除套餐菜品信息
        setmealDishDao.delBySetmealIds(ids);
    }

    @Override
    @Cacheable(value = "setmealCache",key = "#categoryId+'_'+#status") //unless 满足什么条件不缓存
    public List<Setmeal> lst(Long categoryId, Integer status) {

        List<Setmeal> setmealList = setmealDao.lst(categoryId, status);
        return setmealList;
    }

    @Override
    public List<SetmealDishVo> getSetmealDish(Long setmealId) {

        List<SetmealDish> setmealDishList = setmealDishDao.getSetmealDish(setmealId);
        List<SetmealDishVo> setmealDishVoList = setmealDishList.stream().map(setmealDish -> {
            SetmealDishVo vo = new SetmealDishVo();
            BeanUtils.copyProperties(setmealDish, vo);
            // 获取菜品图片
            Dish dish = dishDao.getById(setmealDish.getDishId());
            vo.setImage(dish.getImage());
            return vo;
        }).collect(Collectors.toList());
        return setmealDishVoList;
    }
}
